package ar.edu.itba.it.paw.web.validator;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import ar.edu.itba.it.paw.web.command.EditUserForm;

@Component
public class EditUserFormValidator implements Validator{
	
	@Override
	public boolean supports(Class<?> clazz) {
		return EditUserForm.class.equals(clazz);
	}

	@Override
	public void validate(Object target, Errors errors) {
		EditUserForm form = (EditUserForm)target;
		System.out.println("validating edit user form");
		
		if(form.getFirst_name().equals("")){
			errors.rejectValue("first_name", "empty");
		}
		
		if(form.getLast_name().equals("")){
			errors.rejectValue("last_name", "empty");
		}
		
		if(!form.getOld_password().equals("")){
			if(form.getNew_password().equals("")){
				errors.rejectValue("new_password", "empty");
			}else{
				if(form.getNew_password().length() < 8){
					errors.rejectValue("new_password", "too_short");
				}
				if(form.getRep_new_password().equals("")){
					errors.rejectValue("rep_new_password", "empty");
				}
				else if(!form.getRep_new_password().equals(form.getRep_new_password())){
					errors.rejectValue("rep_new_password", "not_matching");
				}
			}
		}
		else{
			if(!form.getNew_password().equals("") || !form.getRep_new_password().equals("")){
				errors.rejectValue("old_password", "empty");
			}
		}
		
//		if(!form.getNew_password().equals("")){
//			if(form.getNew_password().length() < 8){
//				errors.rejectValue("new_password", "too_short");
//			}
//			else if(form.getOld_password().equals("")){
//				errors.rejectValue("old_password", "empty");
//			} else if(!form.getOld_password().equals(user_service.getUser(form.getUsername()).getPassword())){
//				errors.rejectValue("old_password", "wrong");
//			}
//		}
//		else{
//			if(!form.getOld_password().equals("")){
//				errors.rejectValue("new_password", "empty");
//			}
//		}
	}
	
}
